#include "drv_lcd.h"

/* LCD液晶屏驱动 */



/* extern var */

#if defined(CONFIG_HW_LCD_ID_ST7789)
  #error "LCD Driver ST7789 Not Support!"
#elif defined(CONFIG_HW_LCD_ID_ST7796)
  extern _drv_lcd_opt_t lcd_opt_st7796_t;
  _drv_lcd_opt_t *lcd_opt_t = &lcd_opt_st7796_t;
#elif defined(CONFIG_HW_LCD_ID_ILI9341)
  #error "LCD Driver ILI9341 Not Support!"
#elif defined(CONFIG_HW_LCD_ID_ILI9488)
  #error "LCD Driver ILI9488 Not Support!"
#else
  #error "Lcd Driver Not Select"
#endif



/* export func */

/**
 * @brief 初始化
 * 
 * @return int 
 */
int drv_lcd_init(void)
{
    _drv_lcd_info_t info = { 0 };

    if (lcd_opt_t->init == NULL) return -1;

    lcd_opt_t->init();
   
    drv_lcd_get_info(&info);
    lcd_opt_t->set_direction(info.dir);
    lcd_opt_t->set_window(0, 0, info.width, info.height);

    return 0;
}

/**
 * @brief 去初始化
 * 
 * @return int 
 */
int drv_lcd_deinit(void)
{
    if (lcd_opt_t->init == NULL) return -1;
    
    return lcd_opt_t->deinit();
}

/**
 * @brief 背光开启
 * 
 * @return int 
 */
int drv_lcd_backlight_on(void)
{
    if (lcd_opt_t->set_backlight == NULL) return -1;

    lcd_opt_t->set_backlight(100);

    return 0;
}

/**
 * @brief 背光关闭
 * 
 * @return int 
 */
int drv_lcd_backlight_off(void)
{
    if (lcd_opt_t->set_backlight == NULL) return -1;

    lcd_opt_t->set_backlight(0);

    return 0;
}

/**
 * @brief 背光亮度
 * 
 * @param value 0-100%
 * @return int 
 */
int drv_lcd_backlight_value(uint8_t value)
{
    if (lcd_opt_t->set_backlight == NULL) return -1;

    lcd_opt_t->set_backlight(value);

    return 0;
}

/**
 * @brief 设备屏幕方向
 * 
 * @param dir 
 * @return int 
 */
int drv_lcd_set_direction(_drv_lcd_dir_t dir)
{
    if (lcd_opt_t->set_direction == NULL) return -1;

    if (dir > LCD_DIR_MAX) return -1;

    return lcd_opt_t->set_direction(dir);
}

/**
 * @brief 设置屏幕窗口
 * 
 * @param posx 
 * @param posy 
 * @param width 
 * @param height 
 * @return int 
 */
int drv_lcd_set_window(uint16_t posx, uint16_t posy, uint16_t width, uint16_t height)
{
    if (lcd_opt_t->set_window == NULL) return -1;

    return lcd_opt_t->set_window(posx, posy, width, height);
}

/**
 * @brief 画点
 * 
 * @param posx 
 * @param posy 
 * @param color 
 * @return int 
 */
int drv_lcd_draw_pixel(uint16_t posx, uint16_t posy, lcd_color_t color)
{
    if (lcd_opt_t->draw_pixel == NULL) return -1;

    return lcd_opt_t->draw_pixel(posx, posy, color);
}

/**
 * @brief 填充色块
 * 
 * @param posx 
 * @param posy 
 * @param width 
 * @param height 
 * @param color 填充颜色
 * @return int 
 */
int drv_lcd_fill_color(uint16_t posx, uint16_t posy, uint16_t width, uint16_t height, lcd_color_t color)
{
    if (lcd_opt_t->fill_color == NULL) return -1;

    return lcd_opt_t->fill_color(posx, posy, width, height, color);
}

/**
 * @brief 填充像素流
 * 
 * @param posx 
 * @param posy 
 * @param width 
 * @param height 
 * @param bitmap 像素流
 * @return int 
 */
int drv_lcd_fill_bitmap(uint16_t posx, uint16_t posy, uint16_t width, uint16_t height, lcd_color_t *bitmap)
{
    if (bitmap == NULL) return -1;
    if (lcd_opt_t->fill_bitmap == NULL) return -1;

    return lcd_opt_t->fill_bitmap(posx, posy, width, height, bitmap);
}

/**
 * @brief 填充像素流(指定坐标开始结束位置)
 * 
 * @param posx0 
 * @param posy0 
 * @param posx1 
 * @param posy1 
 * @param bitmap 
 * @return int 
 */
int drv_lcd_fill_bitmap_area(uint16_t posx0, uint16_t posy0, uint16_t posx1, uint16_t posy1, lcd_color_t *bitmap)
{
    if (bitmap == NULL) return -1;
    if (lcd_opt_t->fill_bitmap == NULL) return -1;

    return lcd_opt_t->fill_bitmap_area(posx0, posy0, posx1, posy1, bitmap);
}

/**
 * @brief 清屏
 * 
 * @param color 清屏颜色
 * @return int 
 */
int drv_lcd_clear(lcd_color_t color)
{
    _drv_lcd_info_t info = { 0 };

    lcd_opt_t->get_info(&info);
    return lcd_opt_t->fill_color(0, 0, info.width, info.height, color);
}

/**
 * @brief 获取液晶屏模组信息
 * 
 * @param info 
 * @return int 
 */
int drv_lcd_get_info(_drv_lcd_info_t *info)
{
    if (info == NULL) return -1;
    if (lcd_opt_t->get_info == NULL) return -1;

    return lcd_opt_t->get_info(info);
}



// #if (CONFIG_SHELL_ENABLE)
#if 0

#include "shell.h"

int lcd_bl(int argc, char **argv)
{

    if (argc != 2)
    {
        printf("usage: %s on/off", argv[0]);
        return -1;
    }

    if (strcmp(argv[1], "on") == 0) 
    {
        drv_lcd_backlight_on();
    }
    else if (strcmp(argv[1], "off") == 0)
    {
        drv_lcd_backlight_off();
    }
    else
    {
        printf("usage: %s on/off", argv[0]);
    }

    return 0;
}
SHELL_CMD_EXPORT(lcd_bl, "lcd set backlight");

int lcd_point(int argc, char **argv)
{
    int posx, posy;
    if (argc != 3)
    {
        printf("usage: %s [posx] [posy]", argv[0]);
        return -1;
    }

    posx = atoi(argv[1]);
    posy = atoi(argv[2]);
    if (posx > 480) posx = 0;
    if (posy > 320) posy = 0;

    printf("lcd draw point in [%d, %d]\r\n", posx, posy);
    drv_lcd_draw_pixel(posx, posy, LCD_COLOR_RED);

    return 0;
}
SHELL_CMD_EXPORT(lcd_point, "lcd draw point");

int lcd_fill(int argc, char **argv)
{
    int posx, posy;
    if (argc != 3)
    {
        printf("usage: %s [posx] [posy]", argv[0]);
        return -1;
    }

    posx = atoi(argv[1]);
    posy = atoi(argv[2]);
    if (posx > 460) posx = 0;
    if (posy > 300) posy = 0;

    printf("lcd fill color in [%d, %d]\r\n", posx, posy);
    drv_lcd_fill_color(posx, posy, 20, 20, LCD_COLOR_RED);

    return 0;
}
SHELL_CMD_EXPORT(lcd_fill, "lcd fill color");

int lcd_clear(int argc, char **argv)
{

    drv_lcd_clear(LCD_COLOR_GRAY);

    return 0;
}
SHELL_CMD_EXPORT(lcd_clear, "lcd clear color");


#endif








